import { routerRedux } from 'dva/router'
import {login, getToken, logout} from '../services/login'
import { isLogin, setLoginIn, setLoginOut } from '../utils/login'
import { message } from 'antd'
//message 全局配置
message.config({
  top: 50
})

export default {
  namespace: 'app',
  state: {
    sideCollapse : false,
    current:'1',
    openKeys:[],
    i:'1111111',
    theme : 'light',
    loginload : false,
    collapsed : false,
    user: {
      name: localStorage.getItem('userName')||''
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        const pathname = location.pathname
        if(pathname === '/'){
          setLoginOut();
          dispatch(routerRedux.push({
            pathname: '/login',
            state: {
              nextPathname: location.pathname !== '/login' ? location.pathname : '/', nextSearch: location.search
            }
          }))
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    loginSuccess(state, action) {

      return { ...state, ...action.payload }
    },

    logoutSuccess(state) {
      return { ...state, login: false, userPower: {}, curPowers: [] }
    },

    //改变主题
    changeTheme (state,action) {
      const tmeme = state.theme;
      return {
        ...state,theme: tmeme ==='light' ? 'dark' : 'light',
      }
    },

    toggleCollapsed (state,action) {
      return {
        collapsed: !action.collapsed ,
      }
    },

    //展示加载
    showLoginLoading (state,action) {
      return {
        loginload: true
      }
    },

    //隐藏加载
    hideLoginLoading (state,action) {
      return {
        loginload: false
      }
    },
  },
  effects: {

    //  登录
    *login ({
      payload,
    }, { put, call }) {
        yield put({ type: 'showLoginLoading' })
        yield call(delap,500)
        const logindata = yield call(login, payload)
        yield put({ type: 'hideLoginLoading' })
        const {success, msg, username} = logindata;
        if (success) {
          // yield setLoginIn(payload.username)
          localStorage.setItem('is_login',username);
          localStorage.setItem('userName',payload.username);
          yield put({
            type: 'loginSuccess',
            payload: {
              // theme : 'light',
              user: {
                name: payload.username
              },
            }
          })

        message.success(msg+',欢迎回来,'+'['+username+']',2);
        // yield put(routerRedux.push('/home'))
          location=`${location.origin}/home`;
          }
        // }
      },

    //注销
    *logout({
      payload
    },{call, put}){
      const logoutdata = yield call(logout)
      const { success, msg } = logoutdata;
      if(logoutdata && success){
        // yield setLoginOut();
        yield put({type: 'logoutSuccess'})
        yield put({ type: 'hideLoginLoading' })
        message.success(msg,1)
        localStorage.setItem('is_login','');
        // yield put (routerRedux.push({
        //   pathname: '/login',
        //   state: {nextPathname: location.pathname, nextSearch: location.search }
        // }))
        location=`${location.origin}/login`;
      }
    }
  },
}

//模拟业务逻辑处理时间
const delap = (timeout) =>{
  return new Promise(
    time=>setTimeout(time,timeout))

}
